package me.send.business.service.service;

import java.util.HashMap;
import java.util.Map;

import me.send.business.base.ErrorEntity;
import me.send.business.base.SendVariable;
import me.send.business.base.jpush.JPush;
import me.send.business.service.BaseService;
import me.send.business.service.cons.MessageCons;
import me.send.business.service.mapper.OrderMapper;
import me.send.business.service.mapper.UserMapper;
import me.send.business.service.mapper.ZambiaMapper;
import me.send.business.service.mapper.ZambialogMapper;
import me.send.business.service.pojo.Message;
import me.send.business.service.pojo.User;
import me.send.business.service.pojo.Zambia;
import me.send.business.service.pojo.Zambialog;
import me.send.framework.util.HelperUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jpush.api.JPushClient;

@Service
public class ZambiaService extends BaseService {

	@Autowired
	private ZambiaMapper zambiaMapper;

	@Autowired
	private ZambialogMapper zambialogMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private OrderMapper orderMapper;

	/**
	 * 给订单点个赞
	 * 
	 * 2015年6月10日11:12:43
	 */
	public ErrorEntity zambia_orderid_userid(String devicenumber, Zambia zambia) {
		ErrorEntity entity = error_1000();
		if (isDevicenumber(devicenumber)) {
			// 判断这个对象是否纯在
			if (zambia != null && zambia.getOrderid() > 0
					&& zambia.getUserid() > 0) {
				// 打日志 2015年6月10日11:38:52
				zambia.setLookstatus(0);
				Zambialog zambialog = new Zambialog();
				zambialog.setOrderid(zambia.getOrderid()); // 订单编号
				zambialog.setCreatetime(HelperUtil.getCurrentDate()); // 创建时间
				zambialog.setUserid(zambia.getUserid()); // 用户编号
				// 判断是否点过赞 2015年6月10日11:19:17
				int count = zambiaMapper.count_userid_orderid(zambia);
				// 如果已经点过赞 2015年6月10日11:19:51 那么就 取消
				User user = userMapper.getUserNameById(zambia.getUserid());
				User orderUser = orderMapper.getUserInfoByOrderId(zambia.getOrderid());
				if (count > 0) {
					// 记录到订单赞表的日志表里面 2015年6月10日11:31:08
					zambialog.setStatus(2);
					zambialogMapper.zambialog_save(zambialog);
					// 调用删除方法
					zambiaMapper.delete_userid_orderid(zambia);
					String message = user.getNickname()+"取消对你的点赞";
					JPushClient client = JPush.getUserJPushClient();
					JPush.pushWithAlias(client, message, orderUser.getIsvAccountId());
					Message msg = new Message(message,orderUser.getUserid(),MessageCons.USER_TYPE_USER,MessageCons.MESSAGE_TYPE_ZAN);
					msg.setOrderId(zambia.getOrderid());
					msg.setFromUserId(user.getUserid());
					saveMessage(msg);
				} else {
					// 记录到订单赞表的日志表里面 2015年6月10日11:31:08
					zambialog.setStatus(1);
					zambialogMapper.zambialog_save(zambialog);
					// 调用增加方法
					zambiaMapper.zambia_save(zambia);
					String message = user.getNickname()+"对你发布的订单点赞";
					JPushClient client = JPush.getUserJPushClient();
					JPush.pushWithAlias(client, message, orderUser.getIsvAccountId());
					Message msg = new Message(message,orderUser.getUserid(),MessageCons.USER_TYPE_USER,MessageCons.MESSAGE_TYPE_ZAN);
					msg.setOrderId(zambia.getOrderid());
					msg.setFromUserId(user.getUserid());
					saveMessage(msg);
				}
				// 最后返回成功
				entity = error_1000();
			} else {
				entity = error_1006();
			}
		} else {
			entity = error_1002();
		}
		// 讲所有的对象放在 这个下面
		return entity;
	}

	/**
	 * 根据用户的编号 查询所有 点赞的订单 分页查询
	 * 
	 * 2015年6月10日11:42:31
	 */

	/**
	 * 根据订单编号 查询所有的 用户的 昵称和头像 用户编号
	 * 
	 * 2015年6月10日11:45:01
	 */

	/**
	 * 根据订单编号 查询点赞的数量
	 * 
	 * 2015年6月10日11:45:35
	 */
	public Map<String, Object> count_orderid(int orderid, String devicenumber) {
		Map<String, Object> map = new HashMap<String, Object>();
		ErrorEntity entity = error_1000();
		int count = 0;
		if (isDevicenumber(devicenumber)) {
			// 判断订单编号 是否有效
			if (orderid > 0) {
				count = zambiaMapper.count_orderid(orderid);
			} else {
				entity = error_1006();
			}
		} else {
			entity = error_1002();
		}
		map.put(SendVariable.COMM_ERROR, entity);
		map.put(SendVariable.COMMON_OBJECT, count);
		return map;
	}

}
